import type { Server } from "bun";
import { type Res, res } from "../res";

type TypeRouterParams = {
    [key: string]: { [key: string]: Route; },
};

type TypeRouteFunc = (req: Request, server: Res) => Response | Promise<Response>;


export class Router {

    methods = ['GET', 'POST', 'DELETE'];

    params: TypeRouterParams = { 'HEAD': {}, 'OPTIONS': {}, 'GET': {}, 'PUT': {}, 'PATCH': {}, 'POST': {}, 'DELETE': {} };

    constructor() {



    }

    exe(req: Request): Response | Promise<Response> {

        const url = new URL(req.url);

        const method = req.method;

        const allowed = this.methods.indexOf(method) !== -1;

        if (!allowed) return new Response(null);

        const cb = this.params[method][url.pathname];

        return cb.callback(req, res);

    }

    post(path: string, callback: TypeRouteFunc) {

        this.params['POST'][path] = new Route(path, callback);

    }

    get(path: string, callback: TypeRouteFunc) {

        this.params["GET"][path] = new Route(path, callback);

    }

    delete(path: string, callback: TypeRouteFunc) {

        this.params["DELETE"][path] = new Route(path, callback);

    }

}

export class Route {

    path: string;

    callback: TypeRouteFunc;

    constructor(path: string, callback: TypeRouteFunc) {

        this.path = path;

        this.callback = callback;

    }

}

